package com.sxm.yiwei.activitis;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.BaseIntents;
import com.sxm.yiwei.tools.CropPictures;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MineMsgMsg extends BaseActivity implements View.OnClickListener{
    private ImageView msgmsgback;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private Button upimg;
    private String path1;
    private String s;
    private List<String>imglist = new ArrayList();
    private EditText inname;
    private String name;
    private SharedPreferences surl;
    private SharedPreferences.Editor edit;
    private String imgname;
    private AlertDialog dialog;
    private BaseIntents baseIntents;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_msg_msg);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        surl = getSharedPreferences("imgurl", Context.MODE_PRIVATE);
        edit = surl.edit();
        initMsgMsgView();
        baseIntents = new BaseIntents();

    }

    private void initMsgMsgView() {
        backmain = (ImageView) findViewById(R.id.msgmsg_backmain);
        upimg = (Button) findViewById(R.id.msg_upimg);
        msgmsgback = (ImageView) findViewById(R.id.msg_msg_back);
        inname = (EditText) findViewById(R.id.msg_inname);

        // 设置点击监听

        msgmsgback.setOnClickListener(this);
        upimg.setOnClickListener(this);
        // 删除照片按钮 添加点击监听
        backmain.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.msg_msg_back:
                finish();
                break;

            case R.id.msg_upimg:
                AlertDialog.Builder builder = new AlertDialog.Builder(MineMsgMsg.this,R.style.NoBackGroundDialog);
                View view = LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
                ProgressBar diapro = (ProgressBar) view.findViewById(R.id.pro_dia);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
                upImg();
                break;
        }
    }
    public static final String TAG = "MSG";

    // 上传照片
        private void upImg() {
            name = String.valueOf(inname.getText());
            RequestParams p = new RequestParams(InternetAddressUtis.Url + "/user/updateUserInfo");
            p.addHeader("Content-Type","application/json");
            p.addHeader("userId",userId);
            p.addHeader("appId",appId);
            p.addHeader("token",token);
            /**当Content-Type 为multipart/form-data 时
             *   一定要加  p.setMultipart(true);
             *   表示可为 json 也可为file
             */
            p.setMultipart(true);
            p.addBodyParameter("nickname",name);
            x.http().post(p, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    dialog.dismiss();
                    Toast.makeText(MineMsgMsg.this, "昵称修改成功", Toast.LENGTH_SHORT).show();
                    finish();
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    dialog.dismiss();
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    dialog.dismiss();
                }
            });
        }


}
